<template>
  <div>
    <div style="padding-bottom: 150px;">
      <div class="user-item" v-for="(item, index) in list" :key="index" @click="select(index)">
        <div class="avatar">
          <img :src="item.head_sculpture ? item.head_sculpture : avatar" alt="" width="50">
        </div>
        <div class="name">
          <span>{{ item.name }}</span>
          <img v-if="idx.indexOf(index) !== -1" src="../../../../assets/img/icon-select.png" alt="" width="22">
        </div>
      </div>
    </div>
    <div class="fixed-btn">
      <van-button plain hairline type="info" size="large" @click="showAdd = true">添加</van-button>
      <van-button size="large" type="info" @click="toSubmit" style="margin-top: 16px;">保存</van-button>
    </div>
    <van-dialog
      v-model="showAdd"
      class="close-dialog"
      :before-close="addStudent"
      show-cancel-button>
      <van-field v-model="addForm.name" label="学生姓名" placeholder="请输入学生姓名"/>
      <van-field v-model="addForm.phone" label="电话号码"  placeholder="请输入手机号码"/>
      <!--<van-field v-model="addForm.class_hour" label="剩余课时"  placeholder="请输入剩余课时"/>-->
      <div class="close" slot="title" @click="showAdd = false"><img src="../../../../assets/img/icon-close.png" alt=""></div>
    </van-dialog>
  </div>
</template>

<script>
import api from '@/api'
import {validatePhone} from '@/utils'
export default {
  name: 'select-student-list',
  data () {
    return {
      avatar: require('@/assets/img/icon-avatar.png'),
      list: [],
      idx: [],
      showAdd: false,
      addForm: {
        name: '',
        phone: ''
        // class_hour: ''
      }
    }
  },
  methods: {
    // 获取发送对象 teacherMessageObject
    getList () {
      let params = {
        teacher_id: JSON.parse(sessionStorage.getItem('user')).id
        // teacher_id: '1'
      }
      api.teacherGetStudentr({
        params,
        callback: res => {
          this.list = res.data.data
        }
      })
    },
    addStudent (action, done) {
      if (action === 'cancel') {
        done()
      } else {
        if (!this.addForm.name) {
          this.$toast({
            message: '学生名字不能为空',
            duration: 1000
          })
          return
        } else if (!validatePhone(this.addForm.phone)) {
          this.$toast({
            message: '请填写正确的手机号',
            duration: 1000
          })
          return
        }
        let params = {
          teacher_id: JSON.parse(sessionStorage.getItem('user')).id,
          ...this.addForm
        }
        api.teacherAddStudent({
          params,
          callback: res => {
            if (res.data.error_code === 0) {
              this.showAdd = false
              done(false)
              this.getList()
            }
          }
        })
      }
    },
    select (index) {
      let i = this.idx.indexOf(index)
      if (i === -1) {
        this.idx.push(index)
      } else {
        this.idx.splice(i, 1)
      }
      if (this.$route.query.once) {
        this.toSubmit()
      }
    },

    toSubmit () {
      let result = []
      this.idx.forEach(i => {
        result.push(this.list[i])
      })
      window.sessionStorage.setItem('studentList', JSON.stringify(result))
      this.$router.go(-1)
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>
  .user-item{
    display: flex;
    padding: 10px 0 0 10px;
    align-items: center;
  }
  .user-item .avatar{
    width: 50px;
    height: 50px;
    border-radius: 50%;
    overflow: hidden;
    flex-grow: 0;
    flex-shrink: 0;
  }
  .user-item .name{
    display: flex;
    flex-grow: 1;
    flex-shrink: 1;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid rgba(0,0,0,0.3);
    height: 50px;
    margin-left: 20px;
    font-family: PingFangBlod;
    padding-right: 10px;
  }
  .fixed-btn{
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
  }
</style>
